/**
 *  @author Cornelius Huang
 *  on site controller
 */
app.controller('onsiteController', ['$rootScope', '$scope', 'getCountriesSerivce', function($rootScope, $scope, getCountriesSerivce) {
	
	$scope.countries;
	$scope.flexibility;
	$scope.selectedCountry;
	$scope.onsites = [];

	
	getCountriesSerivce.getCountries().then(function(result) {
		$scope.countries = result;
	}, function(result) {
		console.log(result);
	});
	
	getCountriesSerivce.getFlexibility($rootScope.userInfo.username).then(function(result) {
		$scope.flexibility = result;
	}, function(result) {
		console.log(result);
	});
			
	$scope.selectCountry = function(country) {
		console.log('hello');
		console.log(country.children);
		$scope.selectedCountry = country;
	}
	
	$scope.onchgCall = function(flex) {
		console.log(flex);
		var isFind = false;
		$scope.onsites.forEach(function(onsite) {
			if(onsite.country === flex.country) {
				//TODO: onsite update
				onsite.duration = flex.duration;
				isFind = true;
			}
		});
		if(!isFind) {
			//TODO: onsite push
			$scope.onsites.push(flex);
		}
		//$scope.onsites.push(flex);
	}
	
	$scope.onsiteSubmit = function() {
		getCountriesSerivce.updateFlex($scope.onsites).then(function(result) {
			console.log(result);
		}, function(result) {
			console.log(result);
		});
	}
	
	/*$scope.$watch('onsites.length', function(newvalue, oldvalue) {
		console.log($scope.onsites);
		console.log('$scope.onsites length: ' + newvalue);
	});*/
}]);

app.controller('skillsetController', ['$rootScope', '$scope', 'getSkillService', function($rootScope, $scope, getSkillService) {
	
	$scope.skills;
	$scope.selectedSkills;
	$scope.skillInfo;
	$scope.level;
	$scope.manSkills = [];
	
	getSkillService.getSkills().then(function(result) {
		$scope.skills = result;
	}, function(result) {
		console.log(result);
	});
	
	getSkillService.getSkillInfo($rootScope.userInfo.username).then(function(result) {
		$scope.skillInfo = result;
	}, function(result) {
		console.log(result);
	});
	
	getSkillService.getLevel().then(function(result) {
		$scope.level = result;
	}, function(result) {
		console.log(result);
	});
	
	$scope.selectSkill = function(skill) {
		$scope.selectedSkills = skill.children;
	}
	
	$scope.dataLoad = function(output) {
		var isFind = false;
		console.log(output)
		$scope.manSkills.forEach(function(manSkill) {
			if(manSkill.skill == output.skill) {
				manSkill.level = output.level;
				manSkill.experience = output.experience;
				isFind = true;
			}
		});
		if(!isFind)
			$scope.manSkills.push(output);
	}
	
	$scope.skillUpload = function() {
		getSkillService.updateSkills($scope.manSkills).then(function(result) {
			console.log(result);
		}, function(result) {
			console.log(result);
		})
	}
	
	$scope.$watch('manSkills.length', function(newValue, oldValue) {
		console.log($scope.manSkills);
	});
	
}]);